package com.party.controller;

import com.party.common.MD5;
import com.party.common.Page;
import com.party.model.Document;
import com.party.model.ExcellentParty;
import com.party.model.News;
import com.party.model.SysUser;
import com.party.service.document.DocumentService;
import com.party.service.excellentparty.ExcellentPartyService;
import com.party.service.news.NewsService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Resource
    NewsService newsService;

    @Resource
    DocumentService documentService;

    @Resource
    SysUserService sysUserService;

    @Resource
    ExcellentPartyService excellentPartyService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");

        /**
         * 公告
         */
        List<News> noticeList=newsService.getNewsList("2",7);
        modelAndView.addObject("noticeList",noticeList);

        /**
         * 新闻
         */
        List<News> newsList=newsService.getNewsList("1",8);
//        News news=null;
//        if(newsList!=null){
//            news=newsList.get(0);
//            newsList.remove(0);
//        }
//        modelAndView.addObject("newsObject",news);
        modelAndView.addObject("newsList",newsList);

        /**
         * 党建导读
         */
        List<Document>  partyBuildingList=documentService.getDocumentList("1", 8);
        modelAndView.addObject("partyBuildingList",partyBuildingList);

        /**
         * 支部风采
         */
        List<News> lodgeList=newsService.getNewsList("3",8);
        modelAndView.addObject("lodgeList",lodgeList);

        /**
         * 优秀党员
         */
        ExcellentParty excellentParty=new ExcellentParty();
        Page page=excellentPartyService.getListPageByCustorm(excellentParty,3,1);
        List<ExcellentParty> litExcellentParty=page.getResult();
        modelAndView.addObject("litExcellentParty",litExcellentParty);
        return modelAndView;
    }

    @RequestMapping(value = "/loginCheck")
    @ResponseBody
    public Map<String,Object> loginCheck(
            @RequestParam("username") String username,
            @RequestParam("pwd") String pwd,
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        int status = 0;

        SysUser sysUser = new SysUser();
        sysUser.setPassport(username);
        pwd=MD5.encodeMD5(pwd);
        sysUser = sysUserService.getUser(sysUser);
        if (sysUser != null) {
            if (sysUser.getPwd().equals(pwd)) {
                status = 1;
                session.setAttribute("sysUser", sysUser);
            } else {
                status = -1;
            }
        }
        map.put("msg",status);
        return map;
    }

    @RequestMapping(value = "/loginOut")
    public ModelAndView loginOut(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session
    ) {
        //清除session
        session.removeAttribute("sysUser");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("out", 1);
        modelAndView.setViewName("redirect:/loginReg/login.do");

        return modelAndView;
    }

    @RequestMapping(value = "/showPwd")
    public ModelAndView showPwd(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/user/info/pwdchange");
        return modelAndView;
    }

    /**
     * 密码更改
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/changePwd")
    @ResponseBody
    public Map<String,String> changePwd(
            @RequestParam(value = "uid", required = false) Integer uid,
            @RequestParam(value = "nowPwd", required = false, defaultValue = "") String nowPwd,
            @RequestParam(value = "newPwd", required = false, defaultValue = "") String newPwd,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        Map<String,String> map = new HashMap<String, String>();
        if ((SysUser) session.getAttribute("sysUser") != null) {
            uid = ((SysUser) session.getAttribute("sysUser")).getUid();
        }
        SysUser sysUser=new SysUser();
        sysUser.setUid(uid);
        sysUser.setPwd(newPwd);
        String msg = "";
        if (!"".equals(nowPwd)) {
            SysUser user = sysUserService.getUser(uid);
            nowPwd= MD5.encodeMD5(nowPwd);
            if(user.getPwd().equals(nowPwd)){
                newPwd=MD5.encodeMD5(newPwd);
                user.setPwd(newPwd);
                int num= sysUserService.changePwd(user);
                if (num == 1) {
                    msg = "1";
                } else {
                    msg = "2";
                }
            }else {
                msg = "3";
            }
        }else{
            msg = "2";
        }
        map.put("msg", msg);
        return map;
    }
}
